<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <TreeTools :nodeData="company"
                   root
                   @add="addFn"></TreeTools>
        <!-- 树形组件 -->
        <el-tree :data="list"
                 :props="defaultProps"
                 default-expand-all
                 v-loading="loading"
                 element-loading-text="数据加载中...">
          <template v-slot="{ data }">
            <TreeTools :nodeData="data"
                       @getDepartments="getDepartments"
                       @add="addFn"
                       @edit="editFn"></TreeTools>
          </template>

        </el-tree>
      </el-card>
      <!-- 用.sync替换,子传父要加上updateshowDialog ，@close="showDialog = false"  -->
      <AddDept :showDialog.sync="showDialog"
               :nodeData="nodeData"
               @getDepartments="getDepartments"
               ref="add"></AddDept>
    </div>
  </div>
</template>

<script>
import TreeTools from './components/Treetools.vue'
import { getDepartmentList } from '@/api/departments'
import { listToTree } from '@/utils'
import AddDept from './components/AddDept.vue'
export default {
  name: 'Departments',
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      list: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''     // 添加顶级部门小bug
      },
      loading: false,  // 提供进度条(不显示)
      showDialog: false, // 父传子 提供修改显示(隐藏)
      nodeData: {}  // 当前点击的部门
    }
  },
  created() {
    // 渲染调用
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const data = await getDepartmentList()
      // console.log(data)
      this.company.name = data.companyName
      this.list = listToTree(data.depts)   // 把列表转成树状 封装的listToTree
      //console.log(this.list)
      this.loading = false
    },
    // 添加部门
    addFn(nodeData) {  // 编辑框 显示
      this.showDialog = true
      // console.log(nodeData)
      this.nodeData = nodeData
    },
    // 编辑部门
    editFn(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      //this.$refs.add.form = {...nodeData}  // 回显
      // 用refs找到<AddDept/>组件的一个方法
      this.$refs.add.getInfo(nodeData.id)
    }


  }
}
</script>
<style scoped lang="scss">
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
  .box {
    width: 80px;
  }
}
::v-deep {
  // 修改叶子节点
  // .el-tree-node__expand-icon
  // .el-tree-node__expand-icon.is-leaf::before {

  // }
  // .el-tree-node__expand-icon
  // .el-tree-node__expand-icon.expanded::before {
  //   content: "\e722";
  // }
  // 树状菜单的所有图标
  .el-tree-node__expand-icon {
    font-size: 18px;
    color: #000;
    &::before {
      content: "\e723";
    }
    // 展开的图标
    &.expanded {
      transform: rotate(0deg);
      &::before {
        content: "\e722";
      }
    }
    &.is-leaf {
      color: #000;
      &::before {
        content: "\e7ab";
      }
    }
  }
}
</style>
